﻿using System.Data;
using Google.GData.Analytics;

namespace SSISComponents
{
    class GoogleAnalytics
    {
        /// <summary>
        /// Convert Google.GData.Analytics.DataFeed to DataTable
        /// </summary>
        /// <param name="dataFeed">Google.GData.Analytics.DataFeed</param>
        /// <returns></returns>
        //public static DataTable AsDataTable(DataFeed dataFeed, string Dimensions, string Metrics, char Seperator)
        public static DataTable AsDataTable(DataFeed dataFeed, DataQuery Query)
        {
            DataTable dtGA = new DataTable("GoogleAnalytics");
            char Seperator = ',';

            string[] arrDim = Query.Dimensions.Split(Seperator);
            string[] arrMet = Query.Metrics.Split(Seperator);

            for (int i = 0; i < arrDim.Length; i++)
            {
                DataColumn dcDimension = new DataColumn(arrDim[i], System.Type.GetType("System.String"));
                dtGA.Columns.Add(dcDimension);
            }

            for (int i = 0; i < arrMet.Length; i++)
            {
                DataColumn dcMetrics = new DataColumn(arrMet[i], System.Type.GetType("System.String"));
                dtGA.Columns.Add(dcMetrics);
            }

            foreach (DataEntry entry in dataFeed.Entries)
            {
                DataRow dr = dtGA.NewRow();
                int cc = 0;
                for (int i = 0; i < entry.Dimensions.Count; i++)
                {
                    dr[cc] = entry.Dimensions[i].Value;
                    cc++;
                }

                for (int i = 0; i < entry.Metrics.Count; i++)
                {
                    dr[cc] = entry.Metrics[i].Value;
                    cc++;
                }

                dtGA.Rows.Add(dr);
            }
            return dtGA;
        }

        /// <summary>
        /// Filters can be combined with AND boolean logic as well as with OR boolean logic.
        /// Current Verion combines only with AND.
        /// </summary>
        /// <param name="DimensionsFilter">DimensionsFilter string</param>
        /// <param name="MetricsFilter">MetricsFilter string</param>
        /// <returns></returns>
        public static string FilterCombine(string DimensionsFilter, string MetricsFilter)
        {
            string value = null;
            value = DimensionsFilter;
            if (!string.IsNullOrEmpty(value)) value += ";";
            if (!string.IsNullOrEmpty(MetricsFilter)) value += MetricsFilter;
            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string parseProperty(string Value)
        {
            string strValue = null;
            string[] rows = Value.Split(';');
            int counter = 0;

            for (int i = 0; i < rows.Length - 1; i++)
            {
                string[] property = rows[i].Split('|');
                if (counter > 0) strValue += ";";
                strValue += property[1] + property[2] + property[3];
                counter++;
            }
            return strValue;
        }

    }
}